package org.example.zhihuan.services;


import org.example.zhihuan.services.projectException.CheckedException;
import org.example.zhihuan.util.Db;

import java.text.SimpleDateFormat;
import java.util.Date;

//审核类
public class Check {
    private static int UNCHECKED = 2;  //未审核
    private static int CHECKED_OK = 0; //审核通过
    private static int CHECKED_NO = 1; //审核不通过

    private int checkId;               //审核id
    private int goodsId;               //审核的商品id
    private int adminId;               //审核的管理员id
    private Date date;                  //审核的时间
    private int checkedState;           //审核状态

    private boolean isSaved = false;


    /**
    * 持久化
    */
    public void save(){
        StringBuilder sb = new StringBuilder();
        SimpleDateFormat fm=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(isSaved){
            sb.append("update complaint set ")
                    .append(" goods_id = ").append(goodsId).append(",")
                    .append(" admin_id = ").append(adminId).append(",")
                    .append(" date = '").append(fm.format(date)).append("',")
                    .append(" checked_state = ").append(checkedState).append(",")
                    .append(" where check_id = ").append(checkId);
        }else {
            sb.append("insert into audit(check_id,goods_id,admin_id,date,checked_state) values(").append(checkId).append(",")
            .append(goodsId).append(",").append(adminId).append(",'").append(fm.format(date)).append("',").append(checkedState).append(")");
            isSaved = true;
        }
        Db.execute(sb.toString());
    }

    /**
     * 审核创建
     * @param goodsId
     * @param adminId
     * @param date
     */
    public static void createCheck(int goodsId,int adminId,Date date){
        Check check = new Check();
        check.goodsId = goodsId;
        check.adminId = adminId;
        check.date = date;
        check.save();
    }
}
